#include "aerial\Beam.h"

#include "hge\hgevector.h"

/**
 * @brief shared by all weapon. must be initialized before used
 */
hgeSprite* Beam::spr = NULL;
float Beam::lastFireList[10] = {0.f};

bool Beam::cd(float time, int playerId)
{
	if (time - lastFireList[playerId] < 0.1f) return false;
	return true;
}

Beam::Beam(hgeResourceManager* hgeResMan, float x0, float y0, float _vx, float _vy, float cTime, int playerId)
	: Weapon(hgeResMan, 3.f, playerId), x(x0), y(y0), vx(_vx), vy(_vy), color(0xffffffff)
{
	lastFireList[playerId] = cTime;
	hgeVector a(vx, vy);
	hgeVector n(0, 1);
	rot = vx > 0 ? -a.Angle(&n) : a.Angle(&n);
}

Beam::Beam(hgeResourceManager* hgeResMan, float x0, float y0, float _rot, float cTime, int playerId)
	: Weapon(hgeResMan, 3.f, playerId), x(x0), y(y0), rot(_rot), color(0xffffffff)
{
	lastFireList[playerId] = cTime;
	hgeVector n(0, -1);
	n.Rotate(rot);
	vx = n.x;
	vy = n.y;
}

bool Beam::update(float dt)
{
	if (Weapon::update(dt)) return true;
	if (cLife / tLife < 0.5) 
		color = (int(cLife / tLife / 0.5 * 255) << 24) + 0xffffff;
	x += vx * 500 * dt;
	y += vy * 500 * dt;
	return false;
}

void Beam::render() 
{
	spr->SetColor(color);
	spr->RenderEx(x, y, rot, 0.5f, 0.5f);
}